<template>
  <div>
    <div class="flex-between mb-10px">
      <p class="text-13px text-#646A73">说明：表名过滤掉了站点前缀和模块前缀。</p>
      <el-button type="primary" class="mr-10px" @click="createNewDesign">新的设计</el-button>
    </div>
    <el-table v-loading="loading" :data="dkTableData" style="width: 100%" class="table">
      <el-table-column prop="id" label="id" width="80" />
      <el-table-column prop="table_name" label="表名" />
      <el-table-column prop="table_comment" label="表注释" />
      <el-table-column prop="remark" label="备注" />
      <el-table-column prop="update_time" label="最后保存时间" />
      <el-table-column prop="create_time" label="创建时间" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" type="primary" @click="getForm(scope.row.id)">编辑</el-button>
          <el-button size="small" type="warning" @click="jump(scope.row)">设计</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total,prev, pager, next"
      :total="total"
      :current-page="page"
      :page-size="pageSize"
      @current-change="currentChange"
    />
    <dk-form ref="fc" :form-data="formData" :is-dialog="true" :z-index="10001" @submit="getList(0)" />
  </div>
</template>

<script>
import { getList, getForm, del } from '@/api/crud'

export default {
  name: 'CrudList',
  props: {
    aid: {
      default: 0,
      type: Number
    }
  },
  data() {
    return {
      page: 1,
      pageSize: 10,
      total: 0,
      dkTableData: [],
      searchData: {},
      formWidth: '1000px',
      formData: null,
      loading: true,
      detail: {},
      dialogVisible: false,
      dialogUpgradeSql: false,
      chooseId: 0,
      helpVisible: false
    }
  },
  computed: {},
  mounted() {
    this.getList()
  },
  methods: {
    jump(row) {
      // this.$router.push('/crud/' + row.aid + '?id=' + row.id)
      // location.reload()
      // 这里直接改成新窗口打开
      // window.open(process.env.VUE_APP_BASE_URL + '/gs_developer/#/crud/' + row.aid + '?id=' + row.id)

      // const baseUrl = import.meta.env.VITE_BASE_URL || ''
      // 获取当前地址
      const currentUrl = window.location.origin + window.location.pathname
      const url = `${currentUrl}#/crud/${row.aid}?id=${row.id}`
      window.open(url, '_blank')
    },
    getForm(id) {
      getForm(id).then((res) => {
        this.formData = res.data
        this.$refs.fc.show()
      })
    },
    currentChange(page) {
      this.getList(page)
    },
    getList(page) {
      this.page = page || 0
      this.searchData.aid = this.aid
      getList(this.page, this.pageSize, this.searchData).then((res) => {
        console.log(res)
        this.dkTableData = res.data
        this.total = parseInt(res.msg)
        this.loading = false
      })
    },
    handleDelete(item) {
      this.$confirm('此操作将永久删除该事件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(item.id).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList(0)
        })
      })
    },
    createNewDesign() {
      const url = `/crud/${this.aid}`
      window.open(url, '_blank')
    }
  }
}
</script>

<style scoped lang="scss"></style>
